package com.logicalthinking.socialmall.common.init;

import com.github.chenlijia1111.utils.core.StringUtils;
import com.logicalthinking.socialmall.common.pojo.Constants;
import com.logicalthinking.socialmall.entity.SystemConfig;
import com.logicalthinking.socialmall.service.SystemConfigServiceI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Service;

/**
 * 初始化方法
 * 初始化服务费比例
 *
 * @author chenlijia
 * @version 1.0
 * @since 2019/11/27 0027 上午 9:19
 **/
@Service
public class SocialMallInitFunction implements ApplicationListener<ContextRefreshedEvent> {

    @Autowired
    private SystemConfigServiceI systemConfigService;

    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
        if (contextRefreshedEvent.getApplicationContext().getParent() == null) {
            //初始化服务费比例
            initServiceFee();
        }
    }


    /**
     * 初始化服务费比例
     *
     * @return
     */
    public void initServiceFee() {
        SystemConfig systemConfig = systemConfigService.findByKey(Constants.SERVICE_FEE_RATIO_SYSTEM_CONFIG_KEY);
        if (StringUtils.isEmpty(systemConfig.getConfigValue())) {
            systemConfig.setConfigValue(String.valueOf(Constants.DEFAULT_SERVICE_FEE_RATIO));
            systemConfigService.update(systemConfig);
        }
    }

}
